/*
 * FoldPublicGoodProvisionStrategy.java
 *
 * Created on 8 de Abril de 2011, 23:06
 *
 */

package intopo.game.impl.pgp;

import intopo.game.FoldStrategy;
import java.io.PrintStream;

/**
 *
 * @author <a href="mailto:plmariano@ua.pt">Pedro Lopes da Silva Mariano</a>
 *
 * @version 1.0 8 de Abril de 2011
 */
final public class FoldPublicGoodProvisionStrategy
	implements FoldStrategy<PublicGoodProvisionStrategy>
{
	/**
	 * How many strategies have been counted.
	 */
	private int count;
	/**
	 * Sum of probability to provide the good.
	 */
	private float probability;
	/**
	 * Probability average.
	 */
	private double average;
	/**
	 * Probability standard deviation.
	 */
	private double stddev;
	/**
	 * Creates a new instance of FoldPublicGoodProvisionStrategy
	 */
	public FoldPublicGoodProvisionStrategy ()
	{
		start ();
	}
	public void apply (PublicGoodProvisionStrategy strategy)
	{
		double lastAverage = average;
		count++;
		probability += strategy.getProvideProbability ();
		average = ((count - 1) * average + probability) / count;
		stddev = stddev +  count * (count - 1) * (average - lastAverage) * (average - lastAverage);
	}
	public void print (PrintStream ps)
	{
		ps.print (this.probability);
		ps.print ('\t');
		ps.print (this.stddev / (count - 1));
	}
	public void start ()
	{
		count = 0;
		probability = 0;
		average = 0;
		stddev = 0;
	}
	
}
